from time import time

def bubble(A):
    for j in range(len(A) - 1, -1, -1):
        for  i in range(j):
            if A[i]  > A[i+1]:
                t = A[i]
                A[i] = A[i + 1]
                A[i +1] = t
def bubble_2(A):
    A_sort = 0
    n = len(A) - 1
    while n > 0 and not A_sort:
        A_sort = 1
        for i in range(n):
            if A[i] > A[i + 1]:
                t =A[i]
                A[i] = A[i+1]
                A[i + 1] = t
                A_sort = 0
        n -= 1
def count_time(a, sort):
    begin_time = time()
    sort(a)
    end_time = time()
    return int((end_time - begin_time) * 1000)

for filename in ('m1_sorted.txt', 'm2_reverse.txt', 'm3_shuffle.txt'):
    f = open(filename, 'r')
    data = [int(x) for x in f]
    f.close()
    data2 = data.copy( )
    st1 = count_time(data, bubble)
    st2 = count_time(data2, bubble_2)
    print(filename, 'is sorted by', st1, 'with bubble')
    print(filename, 'is sorted by', st2 , 'with bubble')
